<?php

declare(strict_types=1);

namespace App\Service;

class AftersaleSetService extends BaseService
{

    const TYPE_AFTERSALE_APPLY = 1; // 售后申请期限

    const TYPE_NO_REASON = 2; // 无理由期限

    const TYPE_AFTERSALE_REASON = 3; // 售后原因

    const TYPE_DO = 4; // 处理时限（用户付款时间开始计算）

    const TYPE_ORDER_CANCEL = 5; // 订单取消原因

    const DUTY_PARTY_SHOP = 1;// 卖家责任

    const DUTY_PARTY_USER = 2;// 买家责任

    const MODEL_TYPE_SHOP = 1; //电商模块

    const MODEL_TYPE_FOOD_SEAT = 4; // 美食订座
    const MODEL_TYPE_FOOD_ORDER = 2; // 美食点餐

    const MODEL_TYPE_HOTEL = 3; // 酒店民宿

    const TIME_TYPE_AF_DO = 1;// 售后处理时限
    const TIME_TYPE_USER_RESPONSE = 2;//用户回传时限
    const TIME_TYPE_AUTO_REFUND = 3;// 自动退款
    const TIME_TYPE_ORDER_SEND = 4;// 发货时限
    const TIME_TYPE_AUTO_RECEIVE = 5;//自动确认收货


    /**
     * 获取订单取消原因
     * @param int $type_id
     * @param int $type
     * @param int $model_type
     * @return mixed
     */
    public function getOrderReasonByID(int $type_id, int $type = self::TYPE_ORDER_CANCEL, $model_type = self::MODEL_TYPE_SHOP): array
    {
        $order_set = $this->getModelInstance()
            ->where('type', '=', $type)
            ->where('model_type', '=', $model_type)
            ->find($type_id);
        return empty($order_set) ? [] : $order_set->toArray();
    }

    /**
     * 获取售后原因
     * @param array $af_types
     * @param int $model_type
     * @return array
     */
    public function getAfReason($af_types = [], $model_type = self::MODEL_TYPE_SHOP)
    {

        $reason = $this->getModelInstance()->where(function ($where) use ($af_types, $model_type) {
            if (!empty($af_types)) {
                $where->whereIn('after_sell_type', $af_types);
            }
            $where->where('type', '=', 3)->where('model_type', '=', $model_type);
        })->select(['id', 'name', 'duty_party'])->get();
        return empty($reason) ? [] : $reason->toArray();
    }


    /**
     * 获取订单取消原因
     * @param int $type
     * @param int $model_type
     * @return array
     */
    public function getOrderReason($type = self::TYPE_ORDER_CANCEL, $model_type = self::MODEL_TYPE_SHOP): array
    {
        $order_set = $this->getModelInstance()
            ->where(['type' => $type, 'model_type' => $model_type])
            ->select(['id', 'name'])
            ->get();
        return empty($order_set) ? [] : $order_set->toArray();
    }

    /**
     * 获取时限配置
     * @param int|null $type
     * @param int $model_type
     * @return array
     */
    public function getTimeSet(?int $type = self::TYPE_DO, $model_type = self::MODEL_TYPE_SHOP): array
    {
        $type = $type ?? 0;
        $time_set = $this->getModelInstance()
            ->where(function ($where) use ($type, $model_type) {
                if (empty($type)) {
                    $where->whereIn('type', [
                        self::TYPE_AFTERSALE_APPLY,
                        self::TYPE_NO_REASON,
                        self::TYPE_DO
                    ]);
                } else {
                    $where->where('type', '=', $type);
                }
                $where->where('model_type', '=', $model_type);
            })->get();
        return empty($time_set) || empty($time_set->toArray) ? [] : (empty($type) ? $time_set->toArray() : $time_set->toArray()[0]);
    }

    /**
     * 获取时限配置：1售后处理 2回传单号 3自动退款 4发货时限 5自动确认收货
     * @param int $type 1售后单 2订单
     * @param int $model_type
     * @return array
     */
    public function getTimeTypeSet($type = 1, $model_type = self::MODEL_TYPE_SHOP): array
    {
        $time_type = $this->getModelInstance()
            ->where(function ($where) use ($type, $model_type) {
                if ($type == 1) {
                    $where->whereIn('time_type', [1, 2, 3]);
                } else {
                    $where->whereIn('time_type', [4, 5]);
                }
                $where->where('model_type', '=', $model_type);
            })->get();
        if (empty($time_type)) {
            return [];
        }
        return $time_type->toArray();
    }

}